<%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

<%@page import="java.sql.Connection,
java.sql.PreparedStatement,
java.sql.ResultSet,
java.sql.SQLException,
java.sql.Statement,
java.util.ArrayList,
java.util.Date,
java.util.List"
%>
<%@page import= "edu.gdut.imis.goods.common.*"%>
<%@page import= "edu.gdut.imis.goods.entity.*"%>
<%@page import= "edu.gdut.imis.goods.dao.*"%>
<%@page import= "edu.gdut.imis.goods.business.*"%>
<%@page import= "edu.gdut.imis.goods.business.factory.EBOFactory"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
    <link rel="shortcut icon" href="favicon.ico" />
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/index.css">
<style>
        #ppp{
            margin:20px auto;
            margin-left:160px;
            height:750px;
        }
      #ppp div{
            float: left;
            border:2px solid black;
          
            margin:30px 30px;
          
        }
       #ppp div img{
            height: 250px;
            width: 250px;
            
            margin: 30px 30px;
        }
         h1{
            font-size:60px;
            margin-left:30px;
        }
        
    </style>
</head>
<body>

	<%
	response.setContentType("text/html;charset=utf-8");
	request.setCharacterEncoding("utf-8");
	ArrayList<String> Imgsrc = new ArrayList<String>();
	//取出最后四个
	ArrayList<String> description = new ArrayList<String>();
	ArrayList<Float> price = new ArrayList<Float>();
	ArrayList<Integer> quantity = new ArrayList<Integer>();
	
	ArrayList<String> Imgsrc2 = new ArrayList<String>();
	//取出最后四个
	ArrayList<String> description2 = new ArrayList<String>();
	ArrayList<Float> price2 = new ArrayList<Float>();
	ArrayList<Integer> quantity2 = new ArrayList<Integer>();
	String a1,a2,a3,a4,a5,a6;
	String b1,b2,b3,b4,b5,b6;
	float c1,c2,c3,c4,c5,c6;
	int d1,d2,d3,d4,d5,d6;
	
	float total = 0;
	int uid=0;
	GoodsModel gm = null;
	GoodsItem goodsitem = null;
	
	String id = request.getParameter("id");
	gm = EBOFactory.getGoodsEBO().findByID(id);
	
	
	goodsitem = new GoodsItem(gm,1);
	
	ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
	//进入购物车时要先判断用户是否为已登录状态
	User user = (User) session.getAttribute("user");
//	out.print("<h3>"+user+"</h3>");
//	out.print("<h3>"+user.getId()+"</h3>");
//	int uid = user.getId();
	if(user==null) {
		pageContext.forward("login.html");
	}
	if(user!=null) {
	uid = user.getId();
	}
//	int uid = (int) session.getAttribute("uid");
//	out.print("<h3>"+user.getId()+"</h3>");
//	int uid = user.getId();
    if(cart!=null) {	
    	if(!(gm==null)) {
    	cart.add(goodsitem);
    	//添加商品后遍历
    	}
    	ArrayList<GoodsItem>items = new ArrayList<GoodsItem>(cart.getItems());
    	for(GoodsItem goodsItem:items) {
    		GoodsModel goodm = goodsItem.getGoodsModel();
    		Imgsrc.add(goodm.getImg_src());
    		description.add(goodm.getDescription());
    		price.add(goodm.getPrice());
    		quantity.add(goodsItem.getQuantity()); 	
    		total = total+goodm.getPrice()*goodsItem.getQuantity();
    		
    	}
    	
    	//取出最后六个
    				for(int i=Imgsrc.size()-1;i>Imgsrc.size()-7;i--) {
    		            if(i>=0){
    					Imgsrc2.add(Imgsrc.get(i));
    		            }else{
    		                Imgsrc2.add("空.jpg");
    		            }
    				}
    				
    				 for(int i=description.size()-1;i>description.size()-7;i--) {
    						if(i>=0){
    							description2.add(description.get(i));
    			            }else{
    			            	description2.add("");
    			            }
    					}
    				 for(int i=price.size()-1;i>price.size()-7;i--) {
    						if(i>=0){
    							price2.add(price.get(i));
    			            }else{
    			            	price2.add(Float.parseFloat("0"));
    			            }
    					}
    				 for(int i=quantity.size()-1;i>quantity.size()-7;i--) {
    						if(i>=0){
    							quantity2.add(quantity.get(i));
    			            }else{
    			            	quantity2.add(0);
    			            }
    					}
    	
    	session.setAttribute("cart", cart);
    
    }else {
    	if(!(gm==null)) {
    	ShoppingCart sc = new ShoppingCart();
    	sc.add(goodsitem);
    	
    	//添加商品后遍历
    	ArrayList<GoodsItem>items = new ArrayList<GoodsItem>(sc.getItems());
    	
    	for(GoodsItem goodsItem:items) {
    		GoodsModel goodm = goodsItem.getGoodsModel();
    		//总价
    		total = total+goodsItem.getQuantity()*goodm.getPrice();
    		Imgsrc.add(goodm.getImg_src());
    		description.add(goodm.getDescription());
    		price.add(goodm.getPrice());
    		quantity.add(goodsItem.getQuantity()); 	
    		
    	
    		
    		
    	}
        for(int i=Imgsrc.size()-1;i>Imgsrc.size()-7;i--) {
    		            if(i>=0){
    					Imgsrc2.add(Imgsrc.get(i));
    		            }else{
    		                Imgsrc2.add("空.jpg");
    		            }
    				}
    				
    				 for(int i=description.size()-1;i>description.size()-7;i--) {
    						if(i>=0){
    							description2.add(description.get(i));
    			            }else{
    			            	description2.add("");
    			            }
    					}
    				 for(int i=price.size()-1;i>price.size()-7;i--) {
    						if(i>=0){
    							price2.add(price.get(i));
    			            }else{
    			            	price2.add(Float.parseFloat("0"));
    			            }
    					}
    				 for(int i=quantity.size()-1;i>quantity.size()-7;i--) {
    						if(i>=0){
    							quantity2.add(quantity.get(i));
    			            }else{
    			            	quantity2.add(0);
    			            }
    					}
    	session.setAttribute("cart", sc);
    	
    	
    	}else {
    		pageContext.forward("null.html");
    	}
    	
    }
    a1 = Imgsrc2.get(0);
    a2 = Imgsrc2.get(1);
    a3 = Imgsrc2.get(2);
    a4 = Imgsrc2.get(3);
    a5 = Imgsrc2.get(4);
    a6 = Imgsrc2.get(5);
    b1 = description2.get(0);
    b2 = description2.get(1);
    b3 = description2.get(2);
    b4 = description2.get(3);
    b5 = description2.get(4);
    b6 = description2.get(5);
    c1 = price2.get(0);
    c2 = price2.get(1);
    c3 = price2.get(2);
    c4 = price2.get(3);
    c5 = price2.get(4);
    c6 = price2.get(5);
    d1 = quantity2.get(0);
    d2 = quantity2.get(1);
    d3 = quantity2.get(2);
    d4 = quantity2.get(3);
    d5 = quantity2.get(4);
    d6 = quantity2.get(5);
	String s = "upload/";
	
    
	%>
	 <header class="w">
        <div class="logo">
            <h1><a href="index.html">品优购商城</a></h1>
        </div>
        <form action="/Goods/find" method="POST">
            <input type="hidden" name="submitFlag" value="find">
            <div class="search">
                <input type="text" placeholder="电视机" name="fullName" >
                
                <button type="submit">搜索</button>
                <input type="text" name="id" id="search_id" style="display:none;">
                <!-- <input type="hidden"  name="id"> -->
                
            </div>
        </form>
        <div class="hotword">                        
            <a href="#" class="style_red">优惠购首发</a>
            <a href="#">亿元优惠</a>
            <a href="#">9.9元团购</a>
            <a href="#">美满99减30</a>
            <a href="#">办公用品</a>
            <a href="#">电脑</a>
            <a href="#">通信</a>
        </div>

        <a href="/Goods/showProductServlet?id=00000000" target='_blank'>
            <div class="shopping-car">
                我的购物车&nbsp;&gt;
                <i class="count">8888</i>
            </div>
        </a>
       
        
    </header>
   <h1><a href='/Goods/OrderServlet?id=<%=uid%>'target='_blank'>结算订单</a></h1>

   <form action="OrderServlet">
    <div id="ppp">
     
	<div>
		<%-- <input type="checkbox"> --%>
		<img alt="" src="<%=s+a1%>">
		<div>
		<p><%=b1%></p>
		<span>价格:<%=c1%></span>
		<span>数量:<%=d1%></span>
		<input type="text">备注
		</div>		
	</div>
	
	<div>
		<%-- <input type="checkbox"> --%>
		<img alt="" src="<%=s+a2%>">
		<div>
		<p><%=b2%></p>
		<span>价格:<%=c2%></span>
		<span>数量:<%=d2%></span>
		<input type="text">备注
		</div>		
	</div>
	
	<div>
		<%-- <input type="checkbox"> --%>
		<img alt="" src="<%=s+a3%>">
		<div>
		<p><%=b3%></p>
		<span>价格:<%=c3%></span>
		<span>数量:<%=d3%></span>
		<input type="text">备注
		</div>		
	</div>
	
	<div>
		<%-- <input type="checkbox"> --%>
		<img alt="" src="<%=s+a4%>">
		<div>
		<p><%=b4%></p>
		<span>价格:<%=c4%></span>
		<span value="">数量:<%=d4%></span>
		<input type="text">备注
		</div>		
	</div>
	
	<div>
		<%-- <input type="checkbox"> --%>
		<img alt="" src="<%=s+a5%>">
		<div>
		<p><%=b5%></p>
		<span>价格:<%=c5%></span>
		<span>数量:<%=d5%></span>
		<input type="text">备注
		</div>		
	</div>
	
	<div>
		<%-- <input type="checkbox"> --%>
		<img alt="" src="<%=s+a6%>">
		<div>
		<p><%=b6%></p>
		<span>价格:<%=c6%></span>
		<span>数量:<%=d6%></span>
		<input type="text">备注
		</div>		
	</div>
    
    </div>
</form>
     <h1><a href='index.html'>返回主页</a></h1>
     <h1><a href='/Goods/ClearCartServlet'>清空购物车</a></h1>
</body>
</html>